/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 31.07.2005
 *
 *************************************************/
package fightplan.views;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.jgoodies.binding.value.ValueModel;

import fightplan.business.Category;
import fightplan.business.Tournament;
import fightplan.business.TournamentHolder;
import fightplan.ui.DrawingOptionsPanel;
import fightplan.views.tables.WinnerTable;

@SuppressWarnings("serial")
public class DrawingPanel extends JPanel {

	private ValueModel tournament;
	private WinnerTable winnerTable;
	private DrawingOptionsPanel optionsPanel;
	private FighterChoosePanel fighterPanel;
	private TeamsPanel teamsPanel;
	private JTabbedPane tabbedPane;

	public DrawingPanel(TournamentHolder tournament) {
		super(new BorderLayout());
		this.tournament = tournament;

		tabbedPane = new JTabbedPane();
		winnerTable = new WinnerTable();
		optionsPanel = new DrawingOptionsPanel(tournament);
		fighterPanel = new FighterChoosePanel();
		teamsPanel = new TeamsPanel();

		tabbedPane = new JTabbedPane();
		tabbedPane.addTab("Auslosung", optionsPanel);
		add(tabbedPane, BorderLayout.CENTER);
	}

	public void setCategory(Category category) {
		optionsPanel.setCategory(category);
		int selection = tabbedPane.getSelectedIndex();
		for (int i = tabbedPane.getComponentCount() - 1; i >= 1; i--) {
			tabbedPane.remove(i);
		}
		if (category == null || tournament.getValue() == null)
			return;
		Tournament t = (Tournament) tournament.getValue();
		if (!category.isTeam()) {
			fighterPanel.setCategory(category, t.getFighters());
			tabbedPane.add("Teilnehmer", fighterPanel);
		} else {
			teamsPanel.setCategory(category);
			tabbedPane.add("Teams", teamsPanel);
		}
		if (category != null && category.isPlanned()) {
			for (int i = 0; i < category.getLayout().getSheetCount(); i++) {
				Component sheetComponent = category.getLayout().getSheetComponent(i);
				String title = category.getNameOrDescription();
				if (category.getLayout().getSheetCount() > 1) {
					title += "  -  Blatt " + (i + 1) + "/" + category.getLayout().getSheetCount();
				}
				tabbedPane.addTab("Blatt " + (i + 1), new TitlePanel(title, sheetComponent));
			}
			if (category.getLayout().getEndSheet() != null) {
				tabbedPane.addTab("Finalblatt", new TitlePanel(category.getNameOrDescription() + "  -  Finalblatt",
						category.getLayout().getEndSheet()));
			}
			if (category.getLayout().getFinalrundeSheet() != null) {
				tabbedPane.addTab("Finalrunde", new TitlePanel(category.getNameOrDescription() + "  -  Finalrunde",
						category.getLayout().getFinalrundeSheet()));
			}
			// FightResultsPanel resultsTab = new FightResultsPanel(category);
			// tabbedPane.addTab("Resultate", resultsTab);

			winnerTable.setCategory(category);
			tabbedPane.addTab("Rangliste", winnerTable);
		}
		if (!(selection >= tabbedPane.getTabCount())) {
			tabbedPane.setSelectedIndex(selection);
		}
	}

	private static class TitlePanel extends JPanel {

		public TitlePanel(String title, Component content) {
			super(new BorderLayout());
			JLabel label = new JLabel(title);
			label.setFont(label.getFont().deriveFont(20.0F));
			label.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
			add(label, BorderLayout.NORTH);
			add(content, BorderLayout.CENTER);
		}

	}

}